<template>
  <div id="CarList" ref="CarList">
    <ul class="filterBox">
      <div class="sort" v-show="sortPanel" :style="{height:sortHeight+'px'}">
        <div class="sortCon">
          <p @click="setOrderType(0)">默认排序</p>
          <p @click="setOrderType(1)">车龄由低到高</p>
          <p @click="setOrderType(2)">价格由低到高</p>
          <p @click="setOrderType(3)">表显里程由低到高</p>
        </div>
      </div>
      <div class="sort panelBox" v-show="carAge" :style="{height:sortHeight+'px'}">
        <div class="sortCon">
          <div class="condition">
            <Row :gutter="20">
              <Col :span="8">
                <span>不限</span>
              </Col>
              <Col :span="8">
                <span>1年以内</span>
              </Col>
              <Col :span="8">
                <span>1-3年</span>
              </Col>
              <Col :span="8">
                <span>3-5年</span>
              </Col>
              <Col :span="8">
                <span>5-8年</span>
              </Col>
              <Col :span="8">
                <span>8-10年</span>
              </Col>
              <Col :span="8">
                <span>10年以上</span>
              </Col>
            </Row>
          </div>
          <div class="diyBox">
            <div class="diy">
              <p>自定义车龄（年）</p>
              <span>不限</span>
            </div>
            <Slider v-model="value9" range :max="12"></Slider>
            <Button type="primary" class="sureBtn">确定</Button>
          </div>
        </div>
      </div>
      <div class="sort panelBox" v-show="carPrice" :style="{height:sortHeight+'px'}">
        <div class="sortCon">
          <div class="condition">
            <Row :gutter="20">
              <Col :span="8">
                <span>不限</span>
              </Col>
              <Col :span="8">
                <span>3万以下</span>
              </Col>
              <Col :span="8">
                <span>3-5万</span>
              </Col>
              <Col :span="8">
                <span>5-7万</span>
              </Col>
              <Col :span="8">
                <span>7-10万</span>
              </Col>
              <Col :span="8">
                <span>10-15万</span>
              </Col>
              <Col :span="8">
                <span>15-20万</span>
              </Col>
              <Col :span="8">
                <span>20万以上</span>
              </Col>
            </Row>
          </div>
          <div class="diyBox">
            <div class="diy">
              <p>自定义价格（万元）</p>
              <span>60-80万</span>
            </div>
            <Slider v-model="priceValue" range :max="100"></Slider>
            <Button type="primary" class="sureBtn">确定</Button>
          </div>
        </div>
      </div>
      <li @click="sortPanelShow">
        <span class="el-dropdown-link" :class="sortPanel ? 'activeSort':''">
          排序
          <i
            class="el-icon--right"
            :class="sortPanel ? 'el-icon-arrow-up activeSort':'el-icon-arrow-down'"
          ></i>
        </span>
      </li>
      <li @click="handleBrandShow">
        <span class="el-dropdown-link">
          品牌
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
      </li>
      <li @click="carAgePanelShow">
        <span class="el-dropdown-link" :class="carAge ? 'activeSort':''">
          车龄
          <i
            class="el-icon-arrow-down el-icon--right"
            :class="carAge ? 'el-icon-arrow-up activeSort':'el-icon-arrow-down'"
          ></i>
        </span>
      </li>
      <li @click="carPricePanelShow">
        <span class="el-dropdown-link" :class="carPrice ? 'activeSort':''">
          价格
          <i
            class="el-icon-arrow-down el-icon--right"
            :class="carPrice ? 'el-icon-arrow-up activeSort':'el-icon-arrow-down'"
          ></i>
        </span>
      </li>
      <!-- <li>
        <span class="el-dropdown-link">
          筛选
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
      </li>-->
    </ul>
    <ul class="carBox">
      <mu-load-more
        @refresh="refresh"
        :refreshing="refreshing"
        :loading="loading"
        @load="load"
        loading-text="正在拼命加载中..."
      >
        <li v-for="(item,index) in filterCar" :key="index" v-show="filterCar.length>0">
          <a href="javascript:void(0)" @click="getCarDetail('CarDetail',item.id)">
            <div class="carImgBg" :style="{backgroundImage:'url('+item.fileUrl+')'}"></div>
            <div class="carInfo">
              <p class="type ellipsis-2 PingFang">{{item.carModelName}}</p>
              <p
                class="city ellipsis-1"
              >{{item.carCityName}} / {{item.registerTime}} / {{(item.mileage/10000).toFixed(2)}}万公里</p>
              <span class="price">{{(item.retailPrice/10000).toFixed(2)}}万</span>
              <div class="dataBox">
                <span class="size">{{item.dischargeLevelWord}}</span>
                <span>{{item.updateTime}}</span>
              </div>
            </div>
          </a>
        </li>
        <li class="noData" v-show="filterCar.length<=0">暂无数据</li>
      </mu-load-more>
    </ul>
    <v-brandSelect :brandShow="brandShow" v-on:listenToBrand="brandInfo" ref="brandList"></v-brandSelect>
  </div>
</template>
<script>
import { Slider, Row, Col, Button } from "element-ui";
import { getReq } from "../../assets/js/api";
import { errorRes } from "../../assets/js/check";
import dict from "../../assets/js/utils.js";
import vBrandSelect from "../common/BrandSelect";
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      uid: this.$route.query.id || 0,
      upPrice: this.$route.query.upPrice || "",
      downPrice: this.$route.query.downPrice || "",
      seriesType: this.$route.query.seriesType || "",
      brandName: this.$route.query.brandName || "",
      CarList: [],
      keyWords: "",
      refreshing: false,
      loading: false,
      pageNum: 1,
      pageSize: 50,
      sortPanel: false,
      carAge: false,
      carPrice: false,
      brandShow: false,
      value9: [4, 8],
      priceValue: [0, 100],
      sortHeight: 0,
      orderType: 0, // 0:默认顺序, 1:车龄, 2:价格,3:里程
      carSerieFrom: ""
    };
  },

  components: {
    Button,
    vBrandSelect,
    Slider,
    Row,
    Col
  },
  methods: {
    //禁止滚动
    stop() {
      var mo = function(e) {
        e.preventDefault();
      };
      document.body.style.overflow = "hidden";
      document.addEventListener("touchmove", mo, false); //禁止页面滑动
    },
    /***取消滑动限制***/
    move() {
      var mo = function(e) {
        e.preventDefault();
      };
      document.body.style.overflow = ""; //出现滚动条
      document.removeEventListener("touchmove", mo, false);
    },
    // 获取列表
    getLIst() {
      getReq("/web/goods/queryGoodsForPrefecturePage", {
        prefectureType: this.uid,
        seriesType: this.seriesType,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then(res => {
        let { errcode, content, message } = res;
        if (errcode == 0) {
          if (this.pageNum === 1) {
            this.refreshing = false;
            this.CarList = content.goodsInfo;
          }
          if (this.pageNum > 1) {
            this.loading = false;
            content.goodsInfo.forEach(message => {
              this.CarList.push({
                ...message
              });
            });
          }
          this.CarList.map((item, index) => {
            item.dischargeLevelWord = dict.dischargeLevel[item.dischargeLevel];
          });
        } else {
          errorRes(errcode, message);
        }
      });
    },
    // 点击跳转车辆详情
    getCarDetail(name, orderId) {
      this.$router.push({
        name: name,
        query: {
          orderId: orderId
        }
      });
    },
    // 上拉刷新
    refresh() {
      if (!this.refreshing && !this.loading) {
        this.pageNum = 1;
        this.refreshing = true;
        this.$refs.CarList.scrollTop = 0;
        this.getLIst();
      }
    },
    // 下拉加载
    load() {
      if (!this.refreshing && !this.loading) {
        this.pageNum = this.pageNum + 1;
        this.loading = true;
        this.getLIst();
      }
    },
    // 排序选择框显示
    sortPanelShow() {
      this.carAge = false;
      this.carPrice = false;
      if (this.sortPanel == false) {
        this.stop();
        this.sortPanel = true;
      } else if (this.sortPanel == true) {
        this.move();
        this.sortPanel = false;
      }
    },
    // 车龄选择框显示
    carAgePanelShow() {
      this.sortPanel = false;
      this.carPrice = false;
      if (this.carAge == false) {
        this.stop();
        this.carAge = true;
      } else if (this.carAge == true) {
        this.move();
        this.carAge = false;
      }
    },
    // 车价选择框显示
    carPricePanelShow() {
      this.sortPanel = false;
      this.carAge = false;
      if (this.carPrice == false) {
        this.stop();
        this.carPrice = true;
      } else if (this.carPrice == true) {
        this.move();
        this.carPrice = false;
      }
    },
    // 选择品牌之后的操作
    brandInfo(data) {
      this.brandShow = data;
      this.carSerieFrom = this.$refs.brandList.curSriesdName;
    },
    // 选择品牌
    handleBrandShow() {
      this.brandShow = true;
      this.sortPanel = false;
      this.carAge = false;
      this.carPrice = false;
    },
    //   indexList高度
    sortHeights() {
      let winHeight =
        window.innerHeight ||
        document.documentElement.clientHeight ||
        document.body.clientHeight;
      this.sortHeight = winHeight;
    },
    setOrderType(orderType) {
      this.orderType = orderType;
    }
  },
  computed: {
    isLoading() {
      return this.refreshing || this.loading;
    },
    filterCar() {
      const {
        keyWords,
        CarList,
        orderType,
        carSerieFrom,
        upPrice,
        downPrice,
        brandName
      } = this;
      let filterArr = new Array();

      // 过滤数组
      filterArr = CarList.filter(function(p) {
        if (downPrice !== "" || upPrice !== "") {
          return downPrice <= p.retailPrice && p.retailPrice <= upPrice;
        } else {
          return (
            p.carModelName.indexOf(keyWords) !== -1 &&
            p.carModelName.indexOf(carSerieFrom) !== -1 &&
            p.carModelName.indexOf(brandName) !== -1
          );
        }
      });
      // 排序
      if (orderType) {
        filterArr.sort(function(p1, p2) {
          if (orderType === 1) {
            // 车龄
            return (
              new Date(p2.registerTime).getTime() -
              new Date(p1.registerTime).getTime()
            );
          } else if (orderType === 2) {
            // 车价
            return p1.retailPrice - p2.retailPrice;
          } else if (orderType === 3) {
            // 里程
            return p1.mileage - p2.mileage;
          }
        });
        this.sortPanel = false;
        this.move();
      }
      return filterArr;
    }
  },
  mounted() {
    this.refresh();
    this.sortHeights();
  }
};
</script>
<style type="text/css" lang='scss'>
#CarList {
  padding-top: 44px;
  .filterBox {
    border-top: 1px solid #f8f8f8;
    border-bottom: 1px solid #f8f8f8;
    height: 50px;
    display: flex;
    position: fixed;
    width: 100%;
    background: #fff;
    z-index: 999;
    justify-content: space-between;
    color: #666;
    .activeSort {
      color: #0098ff !important;
    }

    .panelBox {
      .condition {
        padding: 15px;
        span {
          width: 100%;
          border: 1px solid rgba(204, 204, 204, 1);
          border-radius: 2px;
          text-align: center;
          height: 35px;
          line-height: 34px;
        }
        .el-col {
          margin-bottom: 10px;
        }
      }
      .diyBox {
        .diy {
          padding: 0 15px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          background: #f4f6f8;
          height: 30px;
          font-size: 1.083333rem;
          p {
            margin-left: 0;
            border: 0px;
            color: #666;
          }
          span {
            color: #0098ff;
          }
        }
        .el-slider {
          margin: 30px;

          display: flex;
          align-items: center;
        }
        .el-slider__runway {
          height: 2px;
          background: #b5b5b5;
        }
        .el-slider__bar {
          height: 2px;
          background: #0098ff;
        }
        .el-slider__button {
          box-shadow: 1px 1px 3px 2px rgba(0, 0, 0, 0.17);
          border: 0px;
          width: 35px;
          height: 35px;
        }
        .el-slider__button-wrapper {
          top: -19px;
        }
      }
      .sureBtn {
        display: block;
        background: linear-gradient(
          0deg,
          rgba(63, 167, 255, 1),
          rgba(3, 204, 249, 1)
        );
        box-shadow: 0px 10px 10px 0px rgba(78, 173, 254, 0.47),
          0px 2px 0px 0px rgba(222, 251, 252, 0.75);
        border-radius: 20px;
        border: 0px;
        width: 100px;
        margin: 0 auto;
      }
    }

    .sort {
      width: 100%;
      position: absolute;
      background: rgba(0, 0, 0, 0.45);
      top: 49px;
      z-index: 999;
      border-bottom: 1px solid #f8f8f8;
      .sortCon {
        background: #fff;
        padding-bottom: 20px;
      }
      p {
        height: 40px;
        line-height: 40px;
        border-top: 1px solid #f8f8f8;
        margin-left: 15px;
        color: #323232;
        font-size: 1.25rem;
      }
    }
    li {
      border-right: 1px solid #f8f8f8;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 25%;
    }

    .el-icon-arrow-down {
      color: #aaa;
    }
    .mu-raised-button {
      font-size: 1.25rem;
      box-shadow: none;
      width: 100%;
      max-width: auto;
      min-width: auto;
    }
    .mu-raised-button .mu-button-wrapper {
      padding: 0;
    }
  }
  ul.carBox {
    padding: 50px 15px 10px;
    .noData {
      text-align: center;
    }
    li {
      border-bottom: 1px solid rgba(248, 248, 248, 1);
      padding: 10px 0;
      margin-bottom: 0px;
      a {
        padding-top: 10px;
        width: 100%;
        display: flex;
        padding-bottom: 10px;
        height: 127px;
        .carInfo {
          width: 68%;
          float: left;
          height: 105px;
        }
      }

      .carImgBg {
        float: left;
        width: 28%;
        height: 105px;
        margin-right: 2%;
      }
      p.type {
        height: 34px;
        color: #323232;
        font-size: 1.25rem;
        line-height: 1.4rem;
      }
      p.city {
        color: #666;
        // margin: 2px 0;
      }
      span.price {
        color: #0098ff;
        font-size: 1.416667rem;
        font-weight: bold;
        // margin-bottom: 2px;
      }
      .dataBox {
        display: flex;
        justify-content: space-between;
        align-items: center;
        span {
          color: rgba(153, 153, 153, 1);
          font-size: 0.916667rem;
        }
        .size {
          color: #666;
          border-radius: 2px;
          border: 1px solid rgba(102, 102, 102, 1);
          padding: 0.5px 3px;
        }
      }
    }
  }
}
</style>

